// 创建系统store模块
import { Module } from "vuex"
import { ISystemState, IRootState, IQueryPage, IAddPageDate } from "@/types/userType"
import { getUserList, delateUserList, addPageDate, editPageDate } from "@/api/main/system/system"
const system: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      userCount: 0,
      userList: [],
      roleCount: 0,
      roleList: [],
      goodsCount: 0,
      goodsList: [],
      menuCount: 0,
      menuList: [],
      departmentCount: 0,
      departmentList: [],
      categoryCount: 0,
      categoryList: [],
    }
  },
  mutations: {
    changeUserList(state, userList) {
      state.userList = userList
    },
    changeUserCount(state, totalCount) {
      state.userCount = totalCount
    },
    changeRoleList(state, List) {
      state.roleList = List
    },
    changeRoleCount(state, Count) {
      state.roleCount = Count
    },
    changeGoodsList(state, List) {
      state.goodsList = List
    },
    changeGoodsCount(state, Count) {
      state.goodsCount = Count
    },
    changeMenuList(state, List) {
      state.menuList = List
    },
    changeMenuCount(state, Count) {
      state.menuCount = Count
    },
    changeDepartmentList(state, List) {
      state.departmentList = List
    },
    changeDepartmentCount(state, Count) {
      state.departmentCount = Count
    },
    changeCategoryList(state, List) {
      state.categoryList = List
    },
    changeCategoryCount(state, Count) {
      state.categoryCount = Count
    },
  },
  actions: {
    // 发送网路请求,获取数据
    async getPageInfoAction({ commit }, payload: IQueryPage) {
      const pageName = payload.pageName
      console.log(payload)
      // 请求用户列表
      const { list, totalCount } = await getUserList(payload)
      console.log(list)
      console.log(totalCount)
      // 保存用户数据
      commit(`change${pageName}List`, list)
      commit(`change${pageName}Count`, totalCount)
    },

    // 删除数据
    async deletPageInfoAction(context, payload: any) {
      const pageName = payload.pageName
      const id = payload.id
      const res = await delateUserList(pageName, id)
      return res
    },

    // 新增数据
    async addPageDateAction({ dispatch }, payload: IAddPageDate) {
      console.log("新增数据")
      console.log(payload)
      const res = await addPageDate(payload)

      // 请求最新数据
      dispatch("getPageInfoAction", {
        pageName: payload.pageName,
        queryInfo: {
          offset: 0,
          size: 10,
        },
      })
    },
    // 修改用户信息
    async editPageDateAction({ dispatch }, payload: any) {
      console.log("修改数据")
      console.log(payload)
      const res = await editPageDate(payload)

      // 请求最新数据
      dispatch("getPageInfoAction", {
        pageName: payload.pageName,
        queryInfo: {
          offset: 0,
          size: 10,
        },
      })
    },
  },
  getters: {
    // 获取数据列表
    pageDate(state) {
      return (pageName: string) => {
        switch (pageName) {
          case "User":
            return state.userList
          case "Role":
            return state.roleList
          case "Goods":
            return state.goodsList
          case "Menu":
            return state.menuList
          case "Department":
            return state.departmentList
          case "Category":
            return state.categoryList
        }
      }
    },
    // 获取数据数量
    pageDateCount(state) {
      return (pageName: string) => {
        switch (pageName) {
          case "User":
            return state.userCount
          case "Role":
            return state.roleCount
          case "Goods":
            return state.goodsCount
          case "Menu":
            return state.menuCount
          case "Department":
            return state.departmentCount
          case "Category":
            return state.categoryCount
        }
      }
    },
  },
}
export default system
